package com.zhc.excel.controller;
   

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zhc.common.utils.page.PageUtil;
import com.zhc.common.utils.wrap.WrapMapper;
import com.zhc.common.utils.wrap.Wrapper;
import com.zhc.common.web.controller.base.BaseController;
import com.zhc.excel.domain.ImportOperateInfo;
import com.zhc.excel.domain.query.ImportOperateInfoQuery;
import com.zhc.excel.service.ImportOperateInfoService;
import com.zhc.excel.utils.exceptions.ExistedException;

/**
 * ImportOperateInfoController ：导入数据操作日志表; InnoDB free: 10240 kB控制器
 * 
 * @author jinsicao
*/
@Controller
@RequestMapping("importOperateInfo")
public class ImportOperateInfoController extends BaseController {

    @Resource(name="importOperateInfoService")	
    private ImportOperateInfoService importOperateInfoService;

    /** 视图前缀 */
    private static final String viewPrefix ="importOperateInfo";
    
    private static final Log LOG = LogFactory.getLog(ImportOperateInfoController.class);

    /**
     * 分页 查询数据
     * 
     * @param model
     * @param page
     * @param query
     * @return
     */
    @RequestMapping(value = "")
    public String index(Model model, PageUtil page, ImportOperateInfoQuery query) {
        try {
            List<ImportOperateInfo> dataList = importOperateInfoService.queryImportOperateInfoListWithPage(query, page);
            model.addAttribute("dataList", dataList);// 数据集合
            model.addAttribute("query", query);// 查询参数
            model.addAttribute("page", page);// 分页
        } catch (Exception e) {
            LOG.error("importOperateInfo index has error.", e);
        }
        return viewPrefix + "/index";
    }

    /**
     * 导入数据操作日志表; InnoDB free: 10240 kB----添加跳转
     * 
     * @return
     */
    @RequestMapping(value = "addForward")
    public String addForward() {
        return viewPrefix + "/add";
    }

    /**
     * 导入数据操作日志表; InnoDB free: 10240 kB----添加
     * 
     * @param importOperateInfo
     * @return
     */
    @RequestMapping(value = "add")
    @ResponseBody
    public Wrapper<?> add(ImportOperateInfo importOperateInfo) {
        try {
//            importOperateInfo.setCreateUser(getLoginUserCnName());
            if (importOperateInfoService.insert(importOperateInfo)) {
                return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "添加成功！");
            } else {
                return WrapMapper.wrap(Wrapper.ERROR_CODE, "添加失败！");
            }
        } catch (ExistedException e) {
            LOG.warn("importOperateInfo add fail, exist importOperateInfo.");
            return WrapMapper.wrap(Wrapper.ERROR_CODE, "添加失败，已经存在");
        } catch (Exception e) {
            LOG.error("importOperateInfo add has error.", e);
            return WrapMapper.wrap(Wrapper.ERROR_CODE, "添加失败！");
        }
    }

    /**
     * 导入数据操作日志表; InnoDB free: 10240 kB----更新跳转
     * 
     * @param model
     * @param importOperateInfo
     * @return
     */
    @RequestMapping(value = "updateForward")
    public String updateForward(Model model, ImportOperateInfo importOperateInfo) {
        try {
            ImportOperateInfo importOperateInfoResult = importOperateInfoService.getImportOperateInfoById(importOperateInfo.getId());
            model.addAttribute("importOperateInfo", importOperateInfoResult);
        } catch (Exception e) {
            LOG.error("importOperateInfo updateForward has error.", e);
        }
        return viewPrefix + "/update";
    }

    /**
     * 导入数据操作日志表; InnoDB free: 10240 kB----更新
     * 
     * @param model
     * @param importOperateInfo
     * @return
     */
    @RequestMapping(value = "update")
    @ResponseBody
    public Wrapper<?> update(Model model, ImportOperateInfo importOperateInfo) {
        try {
//            importOperateInfo.setUpdateUser(getLoginUserCnName());
            if (importOperateInfoService.update(importOperateInfo)) {
                return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "更新成功！");
            } else {
                return WrapMapper.wrap(Wrapper.ERROR_CODE, "更新失败！");
            }
        } catch (Exception e) {
            LOG.error("importOperateInfo update has error.", e);
            return WrapMapper.error();
        }
    }

    /**
     * 导入数据操作日志表; InnoDB free: 10240 kB----删除
     * 
     * @param importOperateInfo
     * @return
     */
    @RequestMapping(value = "delete")
    @ResponseBody
    public Wrapper<?> delete(ImportOperateInfo importOperateInfo) {
        try {
//            importOperateInfo.setUpdateUser(getLoginUserCnName());
            if (importOperateInfoService.delete(importOperateInfo)) {
                return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "删除成功！");
            } else {
                return WrapMapper.wrap(Wrapper.ERROR_CODE, "删除失败！");
            }
        } catch (Exception e) {
            LOG.error("importOperateInfo delete has error.", e);
            return WrapMapper.error();
        }
    }

    /**
     * 导入数据操作日志表; InnoDB free: 10240 kB----查询-无分页
     * 
     * @param query
     * @return
     */
    @RequestMapping(value = "query")
    @ResponseBody
    public Wrapper<?> query(ImportOperateInfoQuery query) {
        try {
            List<ImportOperateInfo> list = importOperateInfoService.queryImportOperateInfoList(query);
            if (!CollectionUtils.isEmpty(list)) {
                return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, list);
            } else {
                return WrapMapper.wrap(Wrapper.ERROR_CODE, "查询配置信息失败！");
            }
        } catch (Exception e) {
            LOG.error("importOperateInfo query has error.", e);
            return WrapMapper.error();
        }
    }
    
    /**
     * 查询导入数据操作日志表; InnoDB free: 10240 kB详情
     * 
     * @param query
     * @return
     */
    @RequestMapping(value = "detail", method = RequestMethod.GET)
    @ResponseBody
    public Wrapper<?> detail(ImportOperateInfoQuery query) {
        if (null == query || null == query.getId()) {
            return illegalArgument();
        }

        try {
            ImportOperateInfo importOperateInfo = importOperateInfoService.getImportOperateInfoById(query.getId());
            if (importOperateInfo != null) {
                return new Wrapper<ImportOperateInfo>().result(importOperateInfo);
            } else {
                return WrapMapper.wrap(Wrapper.ERROR_CODE, "查询导入数据操作日志表; InnoDB free: 10240 kB详情失败！");
            }
        } catch (Exception e) {
            LOG.warn("detail importOperateInfo has error.", e);
            return error();
        }
    }
}
